# -*- coding: utf-8 -*-
'''
     @Author : LiWenqian
     @File : scan_ip.py
     @Project: scpythonProject
     @CreateTime : 2022/8/11 10:42:32
'''
from multiprocessing import Pool, Queue, Process, Manager
import threading
import os

def scan_ip(i, q):
    ip = f"192.168.2.{i}"
    result = os.system(f"ping {ip} -w 1")
    if not result:
        q.append(ip)

if __name__ == "__main__":
    q = Manager().list()
    p_scan = Pool(6)
    for i in range(255):
        p_scan.apply_async(func=scan_ip, args=(i,q))
    p_scan.close()
    p_scan.join()
    print(q)




# def get_ip(q):
#     ip_lst = []
#     while True:
#         if q.empty():
#             break
#         else:
#             temp = q.get()
#             ip_lst.append(temp)
#     print(ip_lst)

# if __name__ == "__main__":
#     q = Queue()
#     # ip_lst = []
#     # p_scan = Pool(processes=4)
#     for i in range(10):
#         p_scan = threading.Thread(target=scan_ip, args=(i, q))
#         p_scan.start()
#         p_scan.join()
#     p_get = threading.Thread(target=get_ip, args=(q,))
#     p_get.start()
#     # p_scan.close()
#     # p_scan.join()
#     p_get.join()
#     # print(ip_lst)

